//引入ajax
import { chooseList, chooseDay, report,bookDay } from "./request.js"
// 获取元素
let date = document.querySelector(".date");
let select = document.querySelector(".timer");
let status = document.querySelector(".status");
let option = document.querySelector("option");
let index = select.selectedIndex;
let options = select.options;
let btn = document.querySelectorAll("button");
let mask = document.querySelector(".mask")
// 渲染下拉框日期
async function getDat() {
    let data = {
        token: sessionStorage.getItem("token")
    }
    let res = await chooseList(data)
    console.log(data);
    if (res.msg == "操作成功") {
        res.result.list.forEach(function (item, index) {
            select.innerHTML += `<option value="${index}">${item.day_time}</option>`
        })
    }
}
getDat()
// 渲染内容操作
select.addEventListener("change", async function () {
    let data = {
        token: sessionStorage.getItem("token"),
        day_time: select.options[select.selectedIndex].text
    }
    let res = await chooseDay(data)
    status.innerHTML = ""
    if (res.msg == "操作成功") {
        let index = 0
        res.result.list.forEach(function (ele) {
            // console.log(ele);
            let show_name = ele.show_name.split("-")[1] 
            let time2 = +new Date(ele.day_time + " " + show_name)
            let newA = new Date().getTime()
            console.log(new Date(ele.day_time + " " + show_name));
            if (time2 > newA) {
                status.innerHTML += `
                        <span>${res.result.list[index].show_name}</span>
                        <button class="btn" data-time="${res.result.list[index++].show_name}">可预约</button>
                    `
                let btn = document.querySelectorAll(".btn")
                // console.log(btn);
                btn.forEach((item, index) => {
                    item.addEventListener("click", () => {
                        //弹框
                        let dialog = document.querySelector(".dialog")
                        mask.style.display = "block"
                        // console.log(res);
                        if (res.msg == "操作成功") {
                            sessionStorage.setItem("id", res.result.id);
                            sessionStorage.setItem("show_time", res.result.show_time);
                            sessionStorage.setItem("day_time", res.result.day_time);

                            let tip = document.querySelector(".tip")
                            console.log(tip);
                            tip.innerHTML += `
                                <p>日期：${res.result.list[index].day_time}</p>
                                <p>时间：${item.dataset.time}</p>
                                `
                        }
                        let cancel = document.querySelector(".cancel")
                        let confirm = document.querySelector(".confirm")
                        //day_time选择的日期
                        sessionStorage.setItem("day_time", res.result.list[index].day_time);
                        // show_id
                        sessionStorage.setItem("show_id", res.result.list[index].show_id)
                        // show_name当天场次时间
                        sessionStorage.setItem("show_name", res.result.list[index].show_name);
                        confirm.addEventListener("click", async function () {
                            let data = {
                                token: sessionStorage.getItem("token"),
                                day_time: sessionStorage.getItem("day_time"),
                                show_id: sessionStorage.getItem("show_id"),
                            }
                            let res = await bookDay(data)
                            console.log(res);
                            sessionStorage.setItem("id", res.result.id)
                            location.href = "./success.html"
                        })
                        cancel.addEventListener("click", function () {
                            location.href = "./book.html"
                        })
                    })
                })
            } else {
                status.innerHTML += `
                            <span>${res.result.list[index++].show_name}</span>
                            <button class="btn over">已结束</button>
                    `
            }
        })
    }
    timer()
})
async function timer() {
    let data = {
        phone: sessionStorage.getItem("phone"),
        name: sessionStorage.getItem("name")
    }
    let res = await report(data)
    console.log(select.options[select.selectedIndex].text);
    res.result.forEach(function (ele) {
        if (ele.day_time == select.options[select.selectedIndex].text) {
            for (let i = 0; i < status.children.length; i++) {
                // console.log(status.children[i].tagName == "BUTTON");
                if (status.children[i].tagName == "BUTTON") {
                    status.children[i].disabled = true
                    status.children[i].innerHTML = ""
                    status.children[i].innerHTML = `已预约过`
                }
            }
        }
    })
}

